﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Security.Cryptography;
using Administracija.DataLayer;
using Administracija.Repository;

namespace Administracija
{
    public partial class KorisnikRecView : Common.Forms.RecView
    {

        AdministracijaDataContext dc = new AdministracijaDataContext();

        int id;

        RJezik rJezik = new RJezik();
        RKorisnik rKorisnik = new RKorisnik();

        public KorisnikRecView(int id)
        {
            InitializeComponent();
            this.id = id;
        }

        private void KorisnikRecView_Load(object sender, EventArgs e)
        {
            Common.MultiLanguage.Tool.PrevediGrid(gridControl1);
            
            bsJezik.DataSource = rJezik.VratiSve();

            if (this.id == 0)
            {
                bindingSource1.DataSource = rKorisnik.Nov();
                xtraTabPage2.PageEnabled = false;
                cbNeMenjajLozinku.Enabled = false;
            }
            else
            {
                bindingSource1.DataSource = rKorisnik.Izmeni(this.id);
                cbNeMenjajLozinku.Checked = true;
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!cbNeMenjajLozinku.Checked)
            {
                if (String.IsNullOrEmpty(edtLozinka.Text))
                {
                    Common.Tools.MessageError.Show("Morate uneti novu lozinku!");
                    this.DialogResult = DialogResult.None;
                    return;
                }

                if (!String.Equals(edtLozinka.Text, edtLozinka2.Text))
                {
                    Common.Tools.MessageError.Show("Lozinke se ne podudaraju!");
                    this.DialogResult = DialogResult.None;
                    return;
                }

                if (edtLozinka.Text.Length < 6)
                {
                    Common.Tools.MessageError.Show("Minimalna dužina lozinke je 6 karaktera!");
                    this.DialogResult = DialogResult.None;
                    return;
                }
                

                byte[] bytes = Encoding.Unicode.GetBytes(edtLozinka.Text);
                byte[] inArray = HashAlgorithm.Create("SHA1").ComputeHash(bytes);
                string lozinkaHash = Convert.ToBase64String(inArray);

                ((Korisnik)bindingSource1.Current).Lozinka = lozinkaHash;
            }

            try
            {
                bindingSource1.EndEdit();
                this.Index = rKorisnik.Snimi((DataLayer.Korisnik)bindingSource1.Current);
            }
            catch (Exception ex)
            {
                Common.Tools.MessageError.Show(ex.Message);
                this.DialogResult = DialogResult.None;
            }

        }

        private void btnDodaj_Click(object sender, EventArgs e)
        {
            int i = SpisakModula.PrikaziSpisakModula();

            if (i > 0)
            {
                try
                {
                    KorisnikModul n = new KorisnikModul();
                    n.KorisnikId = this.id;
                    n.ModulId = i;
                    dc.KorisnikModuls.InsertOnSubmit(n);
                    dc.SubmitChanges();

                    rKorisnik = new RKorisnik();
                    bindingSource1.DataSource = rKorisnik.Izmeni(this.id);
                }
                catch (Exception ex)
                {
                    Common.Tools.MessageError.Show(ex.Message);
                }
                
            }
        }

        private void btnIzbaci_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount == 0)
            {
                Common.Tools.MessageError.ShowNePostojiSlogZaBrisanje();
                return;
            }

            if (Common.Tools.MessageQuestion.Show("Da li želite da obrišete slog?") == DialogResult.Yes)
            {
                try
                {
                    dc.KorisnikModuls.DeleteOnSubmit(dc.KorisnikModuls.SingleOrDefault(c=>c.KorisnikModulId == ((KorisnikModul)bsPrava.Current).KorisnikModulId));
                    dc.SubmitChanges();
                    rKorisnik = new RKorisnik();
                    bindingSource1.DataSource = rKorisnik.Izmeni(this.id);
                }
                catch (Exception ex)
                {
                    Common.Tools.MessageError.Show(ex.Message);
                }
            }

        }

        private void cbNeMenjajLozinku_CheckedChanged(object sender, EventArgs e)
        {
            if (cbNeMenjajLozinku.Checked)
            {
                edtLozinka.Enabled = false;
                edtLozinka2.Enabled = false;
                edtLozinka.BackColor = Color.Silver;
                edtLozinka2.BackColor = Color.Silver;
            }
            else
            {
                edtLozinka.Enabled = true;
                edtLozinka2.Enabled = true;
                edtLozinka.BackColor = Color.White;
                edtLozinka2.BackColor = Color.White;
            }
        }
    }
}
